package com.copy;

import java.io.*;

/**
 * Version: 1.1
 * Author: TangJun
 * Date: 2024-07-28
 */
public class TxtCopy {
    public static void main(String[] args) {
        String fileParent = "D:\\CScode\\code\\java_code\\2024\\t0728\\src\\com";
        String fileChild1 = "hello.txt";
        String fileChild2 = "copy.txt";
        File fileName = new File(fileParent, fileChild1);
        File fileDest = new File(fileParent, fileChild2);
        BufferedReader buffedReader = null;
        BufferedWriter buffedWriter = null;

        try {
            buffedReader = new BufferedReader(new FileReader(fileName));
            buffedWriter = new BufferedWriter(new FileWriter(fileDest));
            String str;
            while ((str = buffedReader.readLine()) != null) {
                buffedWriter.write(str);
                buffedWriter.newLine();
            }
            System.out.println(fileName.getName() + "文本文件拷贝成功");
        } catch (IOException e) {
            System.out.println("流创建失败");
            e.printStackTrace();
        } finally {
            try {
                if (buffedReader != null) {
                    buffedReader.close();
                }
                if (buffedWriter != null) {
                    buffedWriter.close();
                }
            } catch (IOException e) {
                System.out.println("流关闭失败");
                e.printStackTrace();
            }
        }
    }
}
